package com.yangquan.ayuan.deamonThread;

import java.util.concurrent.TimeUnit;

public class DaemonThreadExample2 {

    public static void main(String[] args) throws InterruptedException {
        // 注册虚拟机钩子，可以在程序正常退出，系统关闭，OOM宕机时被回调
        Runtime.getRuntime().addShutdownHook(new Thread(() -> System.out.println("JVM 进程已结束")));

        Task2 thread = new Task2();
        // 设置守护线程
        thread.setDaemon(true);
        thread.start();
        TimeUnit.SECONDS.sleep(1);
        System.out.println("主线程运行完毕");
    }
}

class Task2 extends Thread {
    @Override
    public void run() {
        while (true) {
            System.out.println("子线程正在运行中");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
